package www.ym.com.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import www.ym.com.common.CustomException;
import www.ym.com.common.R;
import www.ym.com.common.RL;
import www.ym.com.entity.Company;
import www.ym.com.entity.Department;
import www.ym.com.service.CompanyService;

import java.util.List;


@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    /**
     * 添加企业信息
     *
     * @param company
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody Company company) {

        //判重企业
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Company::getEnterpriseName, company.getEnterpriseName());
        int count = companyService.count(wrapper);
        if (count == 1) {
            throw new CustomException("企业已注册，请勿重复注册");
        }

        company.setStatus(0);
        companyService.save(company);
        return R.success("", "企业注册成功");
    }

    /**
     * 修改企业信息
     *
     * @param company
     * @return
     */
    @PutMapping("/update")
    public R<String> update(@RequestBody Company company) {
        companyService.updateById(company);
        return R.success("", "修改成功");
    }

    /**
     * 删除企业信息
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    public  R<String> del(Long id){
        companyService.removeById(id);
        return R.success("","删除成功");
    }

    /**
     * 通过id查询企业详情
     * @return
     */
    @GetMapping("/byId")
    public R<Company> getById(Long id){
        Company byId = companyService.getById(id);
        return R.success(byId,"");
    }

    /**
     * 审核企业信息 // 打回重审
     * @param company
     * @return
     */
    @PostMapping("/examine")
    public R<String>  examine(@RequestBody Company company){
        companyService.updateById(company);
        return R.success("","处理成功");
    }


    /**
     * 企业列表
     *
     * @return
     */
    @PostMapping("/list")
    public RL<Company> list(@RequestBody JSONObject jsonObject) {
        Page<Company> companyPage = companyService.pageCompany(jsonObject);
        List<Company> records = companyPage.getRecords();
        long total = companyPage.getTotal();
        long pageNum = companyPage.getCurrent();
        long pageSize = companyPage.getSize();
        return RL.listSuccess(records, null, total, pageNum, pageSize);
    }


}
